package com.undsf.despot.modles;

import com.undsf.utility.Random;

import java.util.*;

/**
 * Created by Arathi on 2016/10/3.
 */
public class Force {
    protected List<Team> teams;
    protected Map<String, Integer> memberCounter;
    protected Force enemies;

    public Force() {
        teams = new ArrayList<Team>();
        memberCounter = new HashMap<String, Integer>();
    }

    public boolean add(Team t) {
        teams.add(t);
        for(Unit u : t.units) {
            u.force = this;

            if (memberCounter.containsKey(u.name)) {
                int counter = memberCounter.get(u.name)+1;
                memberCounter.put(u.name, counter);
                u.suffix = (char)('A'+counter-1) + "";
            }
            else {
                memberCounter.put(u.name, 1);
            }
        }
        return true;
    }

    public List<Command> selectCommands() {
        List<Command> fCommands = new ArrayList<Command>();
        for (Team t : teams) {
            List<Command> tCommands = t.selectCommands();
            fCommands.addAll(tCommands);
        }
        return fCommands;
    }

    public boolean isFightable() {
        for (Team t : teams) {
            for (Unit u : t.units){
                if (u.hp>0) return true;
            }
        }
        return false;
    }

    public Force getEnemies() {
        return enemies;
    }

    public void setEnemies(Force enemies) {
        this.enemies = enemies;
    }

    public Unit selectAUnit() {
        List<Unit> unitList = new ArrayList<Unit>();
        for (Team t : teams) {
            for (Unit u : t.units){
                // TODO 判断条件
                unitList.add(u);
            }
        }
        Unit target = Random.GetRandomizer().nextElement(unitList);
        return target;
    }

    public List<Unit> selectAllUnit() {
        List<Unit> unitList = new ArrayList<Unit>();
        for (Team t : teams) {
            for (Unit u : t.units){
                // TODO 判断条件
                unitList.add(u);
            }
        }
        return unitList;
    }

    public List<Unit> selectTeam() {
        List<Unit> unitList = new ArrayList<Unit>();
        Team team = Random.GetRandomizer().nextElement(teams);
        for (Unit u : team.units){
            // TODO 判断条件
            unitList.add(u);
        }
        return unitList;
    }
}
